import Vue from 'vue'
import App from './App'
Vue.config.productionTip = false
import wx from 'utils/jswx.js'

import AsyError from './components/async-error'
Vue.component(AsyError.name, AsyError)

Vue.prototype.$wx = wx
Vue.prototype.$host = 'https://rentcat.51zhonglv.cn/DriverPlatform/'
// Vue.prototype.$host = 'http://127.0.0.1:8288/'
Vue.prototype.isLogin = function () {
	let Authorization = uni.getStorageSync('Authorization');
	if(Authorization){
		return true;
	}else{
		uni.reLaunch({
			url: '/pages/login/login'
		})
	}
} 
Vue.prototype.baseRequest = function (action, method, data, suncFun, failFun) { 
    let Authorization = uni.getStorageSync('Authorization');
	let header = {};
	if(Authorization){
		header = {
			"Authorization": Authorization ? Authorization : ''
		};
	}
	uni.request({
		header: header,
        url: Vue.prototype.$host + action,
        dataType: 'json',
        method: method,
		sslVerify: false,
        data: data,
        complete(res) {
          switch (res.statusCode) {
            // 403，405跳转登录
            case 200: {
              res = res.data;
              if (res.success) {
                if (suncFun) {
                  suncFun(res.data, res.info);
                }
              } else if (res.code === 401) {
				// 跳转登录
				uni.showToast({
					title: '请重新登录',
					icon: 'none'
				})
			  } else {
                uni.hideLoading();
                if (failFun) {
                  failFun(res);
                } else {
                  setTimeout(() => {
                    uni.showToast({
                      title: res.info,
                      icon: 'none'
                    });
                    setTimeout(() => {
                      uni.hideToast();
                    }, 3000)
                  }, 0);
                }
              }
              break;
            }
            case 504: {
              setTimeout(() => {
                uni.showToast({
                  title: '系统繁忙，请稍后再试',
                  icon: 'none'
                });
                setTimeout(() => {
                  uni.hideToast();
                }, 1500)
              }, 0);
              break;
            }
            case 405:  
				// 判断是否有邀请码
				let code = window.location.toString().split('&code=')[1];
				if (typeof(code) == "undefined") {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					break;
				}else{
					uni.navigateTo({
						url: '/pages/user/inteuser?id=' + code
					})
					break;
				}
          }
		},
	fail(res) {
	  setTimeout(() => {
		uni.showToast({
		  title: '请求超时，请检查网络或稍后重试',
		  icon: 'none'
		});
		setTimeout(() => {
		  uni.hideToast();
		}, 1500)
	  }, 0);
	}
  })
};  

App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()

 



